import dash
from dash import html, Input, Output, State, dcc
import uuid
import random
import feffery_antd_components as fac
import feffery_utils_components as fuc

task_info_demo = [
    {
        'task_id': str(uuid.uuid4()),
        'task_memo': f'任务{i}备注信息',
        'task_status': random.choice(['success', 'processing', 'error'])
    }
    for i in range(999)
]

app = dash.Dash(__name__, suppress_callback_exceptions=True)

app.layout = html.Div(
    [
        html.Div(
            [
                dcc.Store(
                    id='task-info-store',
                    data=task_info_demo
                ),

                html.Div(
                    id='tasks-container'
                ),

                fac.AntdPagination(
                    id='pagination',
                    defaultPageSize=10,
                    showSizeChanger=False
                )
            ],
            style={
                'width': '1000px',
                'margin': '0 auto',
                'border': '1px solid #d9d9d9',
                'padding': '25px'
            }
        )
    ],
    style={
        'padding': '50px'
    }
)


@app.callback(
    Output('pagination', 'total'),
    Input('task-info-store', 'data')
)
def initial_page_total(pathname):
    return len(task_info_demo)


@app.callback(
    Output('tasks-container', 'children'),
    Input('pagination', 'current'),
    State('task-info-store', 'data')
)
def update_current_page_tasks(current, data):
    if current:
        return fac.AntdSpace(
            [
                fac.AntdRow(
                    [
                        fac.AntdCol(
                            fac.AntdText('任务id'),
                            span=10,
                            style={
                                'display': 'flex',
                                'alignItems': 'center',
                                'justifyContent': 'center',
                                'height': '40px'
                            }
                        ),
                        fac.AntdCol(
                            fac.AntdText('任务备注'),
                            span=6,
                            style={
                                'display': 'flex',
                                'alignItems': 'center',
                                'justifyContent': 'center',
                                'height': '40px'
                            }
                        ),
                        fac.AntdCol(
                            fac.AntdText('任务状态'),
                            span=2,
                            style={
                                'display': 'flex',
                                'alignItems': 'center',
                                'justifyContent': 'center',
                                'height': '40px'
                            }
                        ),
                        fac.AntdCol(
                            fac.AntdText('任务操作'),
                            span=6,
                            style={
                                'display': 'flex',
                                'alignItems': 'center',
                                'justifyContent': 'center',
                                'height': '40px'
                            }
                        ),
                    ],
                    style={
                        'borderBottom': '1px solid #f1f2f4'
                    }
                ),
                *[
                    fac.AntdRow(
                        [
                            fac.AntdCol(
                                fac.AntdText(
                                    task['task_id'],
                                    copyable=True
                                ),
                                span=10,
                                style={
                                    'display': 'flex',
                                    'alignItems': 'center',
                                    'justifyContent': 'center',
                                    'height': '40px'
                                }
                            ),
                            fac.AntdCol(
                                fac.AntdText(
                                    task['task_memo'],
                                    copyable=True
                                ),
                                span=6,
                                style={
                                    'display': 'flex',
                                    'alignItems': 'center',
                                    'justifyContent': 'center',
                                    'height': '40px'
                                }
                            ),
                            fac.AntdCol(
                                fac.AntdBadge(
                                    dot=True,
                                    status=task['task_status']
                                ),
                                span=2,
                                style={
                                    'display': 'flex',
                                    'alignItems': 'center',
                                    'justifyContent': 'center',
                                    'height': '40px'
                                }
                            ),
                            fac.AntdCol(
                                fac.AntdSpace(
                                    [
                                        fac.AntdButton(
                                            '删除',
                                            disabled=task['task_status'] == 'processing',
                                            danger=True,
                                            type='primary'
                                        ),
                                        fac.AntdButton(
                                            '终止',
                                            disabled=task['task_status'] != 'processing',
                                            type='primary'
                                        ),
                                        fac.AntdButton(
                                            '重启',
                                            disabled=task['task_status'] == 'processing'
                                        )
                                    ]
                                ),
                                span=6,
                                style={
                                    'display': 'flex',
                                    'alignItems': 'center',
                                    'justifyContent': 'center',
                                    'height': '40px'
                                }
                            ),
                        ],
                        style={
                            'borderBottom': '1px solid #f1f2f4'
                        }
                    )
                    for task in data[(current - 1) * 10: current * 10]
                ]
            ],
            direction='vertical',
            style={
                'width': '100%',
                'paddingBottom': '50px'
            }
        )


if __name__ == '__main__':
    app.run(debug=True)